const api = require("../../../utils/cloudAPIUtils");
const app = getApp();

Page({
    data: {
        avatarUrl:null,
        upUrl:'',
        nickName:null,
        showTxt:false
    },

    //转base64
    _toBase64OFPng(url){
        return new Promise((resolve)=>{
            wx.getFileSystemManager().readFile({
                filePath:url,
                encoding:'base64',
                success(res){
                    resolve('data:image/jpeg;base64,'+res.data);
                },
                fail(err){
                    console.log(err.errMsg)
                }
            })
        })
    },
    
    //选择头像
    onChooseAvatar(e){
        console.log(e);
        const { avatarUrl } = e.detail;
        this.setData({avatarUrl})
        const self = this;
        self._toBase64OFPng(avatarUrl).then(base64=>{
            api.post(api.url.userImgUp,{
                content:base64
            },res=>{
                console.log(res);
                if(res.data.code==200){
                    wx.showToast({
                      title: '上传头像成功',
                    })
                    self.setData({
                        upUrl:res.data.body.imgPath
                    })
                }else{
                    wx.showToast({
                        title: '上传头像失败',
                        icon:'error'
                    })
                    // this.setData({avatarUrl:null});
                }
            })
        })
    },
    //输入
    inputNick(e){
        console.log(e)
        this.setData({
            nickName:e.detail.value
        })
    },

    btnSubmit(){
        if(!this.data.upUrl && !this.data.nickName){
            wx.showModal({
              title: '温馨提示',
              content: '未更新头像或昵称',
              showCancel:false,
            })
            return;
        }
        const self = this;
        api.post(api.url.userInfoUp,{
            nickname:self.data.nickName,
            faceImg:self.data.upUrl
        },res=>{
            console.log(res)
            if(res.data.code==200){
                wx.showToast({
                    title: '更新成功！',
                    icon:'success',
                    success(){
                        app.globalData.userInfo = '';
                        setTimeout(()=>{
                            wx.reLaunch({
                                url: '/pages/index/index',
                            })
                        },800)
                    }
                })
            }else if(res.data.msg){
                wx.showModal({
                  content: res.data.msg,
                  showCancel:false
                })
            }else{
                wx.showToast({
                  title: '保存失败',
                  icon:'error',
                  mask:true
                })
            }
        })
    },

    onLoad(options) {
        let info = app.globalData.userInfo;
        if(info && info.loginToken){
            let img = null;
            if(info.memberFaceImg){
                let array = info.memberFaceImg.split('res.judaozhineng.com');
                if(array && array.length == 2){
                    img = array[1];
                }
            }
            this.setData({
                avatarUrl:info.memberFaceImg,
                upUrl:img?img:'',
                nickName:info.memberName,
                showTxt:options.show?true:false
            })
        }else{
            wx.showToast({
              title: '登陆失效',
              icon:'error',
              mask:true
            })
            setTimeout(function(){
                wx.redirectTo({
                    url:'/pages/index/index'
                });
            },700);
        }
    },
})